package com.songlanyun.modules.marketing.groupbuy.controller.api;

import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;
import com.songlanyun.modules.account.controller.common.AbstractApiController;
import com.songlanyun.modules.marketing.groupbuy.entity.GroupBuyTeam;
import com.songlanyun.modules.marketing.groupbuy.model.vo.GroupBuyOrderVO;
import com.songlanyun.modules.marketing.groupbuy.service.GroupBuyTeamMemberService;
import com.songlanyun.modules.marketing.groupbuy.service.GroupBuyTeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 拼团活动
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2020-11-27 14:03:39
 */
@RestController
@RequestMapping("api/groupbuy/team")
public class GroupBuyTeamApiController extends AbstractApiController {
    @Autowired
    private GroupBuyTeamService groupBuyTeamService;
    @Autowired
    private GroupBuyTeamMemberService groupBuyTeamMemberService;


    /**
     * 查询我参与的团订单
     *  params参数说明
     *  status (1:待支付，2：组团中，3组团成功，4时间到了人员不足组团失败)
     *  page 当前第几页（默认第一页）
     *  limit 每页多少条 （默认每页10条）
     */
    @GetMapping("/my")
    public R myJoinTeam(@RequestParam Map<String, Object> param) {
        param.put("user_id",getUserId(true));
        PageUtils page = groupBuyTeamMemberService.queryMyJoinTeamOrderPage(param);
        return R.ok().put("page", page);
    }

    /**
     * 查询我的团订单详情
     */
    @GetMapping("/teamorder/{memberId}")
    public R teamOrder(@PathVariable("memberId") Long memberId) {
        GroupBuyOrderVO vo = groupBuyTeamMemberService.getByMemberOrder(getUserId(true),memberId);
        return R.ok().put("data", vo);
    }



    /**
     * 查询团详情
     */
    @GetMapping("/info/{teamId}")
    public R teamInfo(@PathVariable("teamId") Long teamId) {
        GroupBuyTeam groupBuyTeam = groupBuyTeamService.loadById(teamId, true);
        groupBuyTeam.loadLeaderUserInfo();
        groupBuyTeam.loadMemberUserInfos();
        return R.ok().put("data", groupBuyTeam);
    }



    /**
     * 拼团商品中显示，进行中的团
     */
    @GetMapping("/list/{shopId}/{activityGoodsId}")
    public R loadTeamsByAGId(@PathVariable("shopId") Long shopId,@PathVariable("activityGoodsId") Long activityGoodsId) {
        List<GroupBuyTeam> list = groupBuyTeamService.getListByAGIdAndShopId(shopId,activityGoodsId);
        list.forEach(o->{
            o.loadLeaderUserInfo();
            o.loadMemberUserInfos();
        });
        return R.ok().put("list", list);
    }














}
